#include "display.h"
#include "record.h"
#include "reports.h"
#include "usb.h"
#include "version.h"

static uint buf[32]={RPT_ACCREC,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,
	RPT_ACCREC,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,};
static ubyte mod;

//------------------------------------------------------------------------------

ubyte reports_prepare(int x,int y,int z,ubyte stt){
	static uint *ptr=buf+1;
	*ptr++=((x>>6)&0x000003ff)+((y<<5)&0x1ff800)+((z<<16)&0xffc00000);
	if(ptr==buf+16){
		mod=1;
		ptr++;
	}else if(ptr>=buf+32){
		mod=2;
		ptr=buf+1;
	}
	return COUNT_SEARCHING;
}
void reports_send(void){
	static ubyte idx;
	if(!usb_busy())switch(mod){
	case 1:
		sendReport(buf);
		break;
	case 2:
		sendReport(buf+16);
		break;
	default:
		if(!idx){
			uint v=RTC_GetCounter();
			report.tickLoV=v;
			report.tickHiV=v>>16;
			report.offsetLoV=savRam.timeOfs;
			report.offsetHiV=savRam.timeOfs>>16;
			report.currentLoV=curSteps;
			report.currentHiV=curSteps>>16;
			sendReport(&report);
		}else{
			REPORT *p=record_piece();
			if(p)
				sendReport(p);
		}
		idx=(idx+1)&7;
		return;
	}
	mod=0;
}